package com.ssyt.csgl.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.ssyt.csgl.po.User;
import com.ssyt.csgl.service.UserService;

/**
 * 用户操作
 * 
 * @author tyler UserController 2017年7月5日
 */
@Controller
@RequestMapping("/user/")
public class UserController {
	public UserController() {
		System.out.println("UserController 实例化 ");
	}

	@Autowired
	private UserService userService;

	/**
	 * 用户登陆验证
	 * http://blog.csdn.net/zzg1229059735/article/details/51566563
	 * @param user
	 * @param request
	 * @param session
	 * @param attr
	 * @return
	 */
	@RequestMapping("login")
	public String userLogin( User user,HttpServletRequest request,
			HttpSession session,
			RedirectAttributes attr) {
		if (user.getUserEmail() != null
				&& user.getUserEmail().trim().length() > 0
				&& user.getUserPassword() != null
				&& user.getUserPassword().trim().length() > 0) {
			user = userService.selectUser(user);
			if (user != null) {
				System.out.println("进入controller层");
				session.setAttribute("user", user);
				session.setAttribute("u", "是不是传过来了");
				System.out.println(user);
				return "index";
			} else {
				attr.addFlashAttribute("message", "账号或者密码错误！");
				return "redirect:/login.jsp";
			}
		} else {
			System.out.println("adfaf");
			attr.addFlashAttribute("message", "请输入账号密码!");
			return "redirect:/login.jsp";
		}
	}

	/**
	 * 用户退出操作
	 * 
	 * @param user
	 * @param request
	 * @param session
	 * @return
	 */
	@RequestMapping("out")
	public String userOut(@ModelAttribute User user,
			HttpServletRequest request, HttpSession session) {
		return "redirect:/login.jsp";

	}
	@RequestMapping("fx")
	@ResponseBody
	public ModelAndView fx(){
		return new ModelAndView("demo_page1");
		
	}

}
